from autoTest_prtcApi.core.user_api_utils import UserApiUtils
from autoTest_prtcApi.utils.get_path import get_photoFile_path
from autoTest_prtcApi.utils.yaml_utils import write_yaml, read_yaml

# 公共请求头
headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
class UserApi:

    # 实例化UserApiUtils
    userApiUtils = UserApiUtils()

    # 注册API
    def register_api(self,username,passwd,phone):
        # 注册接口请求头

        # 请求参数
        register_params = {
            'user_name': username,
            'passwd': passwd,
            'phone': phone
        }
        # 接收接口返回的数据
        return self.userApiUtils.register_utils(headers=headers, json=register_params)

    # 登录API
    def login_api(self,username,password):
        # 请求参数
        login_params = {
            'passwd': password,
            'phone': username
        }
        # 接收接口返回的数据
        res =  self.userApiUtils.login_utils(headers=headers, json=login_params)

        # 创建数据并写入yaml文件
        if res.json()['code'] == 0:
            content = {
                'login_userId': res.json()['data']['userId'],
                'login_token': res.json()['data']['token']
            }
            write_yaml("temp.yaml", content)
        return res

    # 修改昵称API
    def modifyUserNick_api(self):
        # 从yaml文件拿取数据
        content = read_yaml('temp.yaml')
        modifyUserNick_headers = {
            'userId': str(content['login_userId']),
            'token': content['login_token']
        }
        # 请求参数
        modifyUserNick_params = {
            'nickName': '笑嘻嘻'
        }
        # 接收接口返回的数据
        return self.userApiUtils.modifyUserNick_utils(headers=modifyUserNick_headers, data=modifyUserNick_params)

    # 修改头像API
    def modifyHeadPic_api(self):
        # 从yaml文件拿取数据
        content = read_yaml('temp.yaml')
        # 请求头
        modifyHeadPic_headers = {
            'userId': str(content['login_userId']),
            'token': content['login_token']
        }
        # 请求参数(读取文件)
        file_url = get_photoFile_path('photo_aaa.jpg')
        modifyHeadPic_file = {"photo": open(file_url, 'rb')}
        # 接收接口返回的数据
        return self.userApiUtils.modifyHeadPic_utils(headers=modifyHeadPic_headers, files=modifyHeadPic_file)



